package com.example.farmer.Activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.farmer.Database.UserMessage;
import com.example.farmer.Entity.User;
import com.example.farmer.R;

public class LoginActivity extends AppCompatActivity {
    private UserMessage userMessage;
    private EditText numberEditText;
    private EditText passwordEditText;
    private Button loginButton;
    private Button signButton;
private CheckBox showPasswordCheckBox;
    @SuppressLint("MissingInflatedId")
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        numberEditText = findViewById(R.id.numberEditText);
        passwordEditText = findViewById(R.id.passwordEditText);
        loginButton = findViewById(R.id.loginButton);
        signButton = findViewById(R.id.signButton);
        userMessage = new UserMessage(this);
        showPasswordCheckBox = findViewById(R.id.showPasswordCheckBox);

        showPasswordCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    // 显示密码
                    passwordEditText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);

                } else {
                    // 隐藏密码
                    passwordEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                passwordEditText.setSelection(passwordEditText.length()); // 将光标置于文本末尾
            }

        });

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userNumber = numberEditText.getText().toString().trim();
                String password = passwordEditText.getText().toString().trim();

                //使用SharedPreferences：在登录成功后将学生ID和学生姓名信息保存到SharedPreferences中，然后在其他界面中读取这些信息。
                SharedPreferences sharedPreferences = getSharedPreferences("loginNumber" , Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("userNumber" , userNumber);
                editor.apply();//使用apply异步保存，使用commit同步保存

                // 电话号码格式校验（假设学号为数字且有特定长度）
                if (!userNumber.matches("\\d{11}")) {
                    Toast.makeText(LoginActivity.this, "电话号码应为11位数字", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (password.isEmpty()) {
                    Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }

                // 密码强度校验（可根据需要调整）
                if (password.length() < 6) {
                    Toast.makeText(LoginActivity.this, "密码至少需要6位字符", Toast.LENGTH_SHORT).show();
                    return;
                }

                // 尝试登录
                User user = userMessage.getUser(userNumber, password);
                if (user != null) {
                    // 额外确认姓名匹配，尽管这通常在getStudent逻辑中已涵盖，这里作为演示
                    if (user.getUserNumber().equals(userNumber)) {
                        Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    } else {
                        Toast.makeText(LoginActivity.this, "手机号与密码不匹配", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(LoginActivity.this, "手机号或密码错误", Toast.LENGTH_SHORT).show();
                }
            }
        });


        signButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this , SignActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

}
